package ms.jasim.model.editor.actions;

import ms.jasim.framework.IMessageConsole;
import ms.jasim.framework.JasimFramework;
import ms.jasim.model.Actor;
import ms.jasim.model.Capability;
import ms.jasim.model.Goal;
import ms.jasim.model.JasimModel;
import ms.jasim.model.ModelObject;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class ValidateModelAction implements IWorkbenchWindowActionDelegate, IEditorActionDelegate {

	private JasimModel jasimModel;
	private String modelFile;
	private IWorkbenchWindow window;

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		// TODO Auto-generated method stub
		this.window = window;
	}

	@Override
	public void run(IAction action) {
		if (jasimModel != null) {
			JasimModel model = jasimModel;

			StringBuilder sb = new StringBuilder();
			
			IMessageConsole out = JasimFramework.getService(null, IMessageConsole.class);
			
			// validate each leaf goal has at least 1 provider
			for (Goal g: model.getGoals())
				if (g.getDecomposition().size() == 0) {
					boolean found = false;
					for (Actor a: model.getActors())  {
						for (Capability cap: a.getCapability()) {
							if ((found =  (cap.getGoal() == g))) {
								out.write(String.format("Actor %s provide goal %s\r\n", a.getName(), g.getName()));
								break;
							}
						}
						if (found) 
							break;
					}
					if (!found)
						sb.append(String.format("Goal [%s] has no provider.\r\n", g.getName()));
				}
			
			String msg = sb.length() > 0 ? sb.toString() : "Validate OK";
			
			MessageDialog.openInformation(null, "Input Checker", msg);				
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof TreeSelection) {
			TreeSelection ts = (TreeSelection) selection;
			Object first = ts.getFirstElement();
			
			this.jasimModel = null;
			
			if (first instanceof XMIResource) {
				EList<EObject> resList = ((XMIResource)first).getContents();
				this.modelFile = ((XMIResource)first).getURI().toFileString();
				this.jasimModel = resList.size() > 0 && resList.get(0) instanceof JasimModel ? (JasimModel) resList.get(0) : null;
			}
			else if (first instanceof ModelObject) {
				EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(first);
				if (domain != null) {
					EList<Resource> list = domain.getResourceSet().getResources();
					EObject root = list.get(0).getContents().get(0);
					if (root instanceof JasimModel) {
						this.jasimModel = (JasimModel) root ;
						this.modelFile = list.get(0).getURI().toFileString();
					}
				}
			}
		}
		else 
			this.jasimModel = null;
		
		action.setEnabled(this.jasimModel != null);
	}

	
	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// TODO Auto-generated method stub
		
	}
 

}
